package com.ruoyi.common.utils;

import com.ruoyi.common.domain.excel.EmployeeExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;

/**
 * 描述：针对于员工档案信息进行导出
 * @author Cymal
 * @date 2024-06-20
 */
@Slf4j
public class EmployeeFileExportUtil {


    public static final String INFO_SUFFIX = "员工信息";

    public static void export(HttpServletResponse response, EmployeeExcelVo excelVo) {
        Workbook workbook = new XSSFWorkbook();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=exported_data.xlsx");
        try {
            // 1. 创建一个新的工作薄
            // 2. 选择工作表
            Sheet sheet = workbook.createSheet(excelVo.getUsername() + INFO_SUFFIX);
            // 3. 获得头像的单元格
            CellRangeAddress avatarAddress = new CellRangeAddress(
                    excelVo.getFirstAvatarMergedRow(),
                    excelVo.getFirstAvatarMergedColumn(),
                    excelVo.getLastAvatarMergedRow(),
                    excelVo.getLastAvatarMergedColumn());
            sheet.addMergedRegion(avatarAddress);
            Row row = sheet.createRow(excelVo.getFirstAvatarMergedRow());
            Cell cell = row.createCell(excelVo.getFirstAvatarMergedColumn() + 1);
            cell.setCellValue("合并单元格");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("导出员工信息失败！", e);
        } finally {
            IOUtils.closeQuietly(workbook);
        }
    }

}
